Improved circuit implementation of the HHL algorithm and its simulations on QISKIT

In 2019, Yonghae Lee et al. combined the circuit implementation of the Harrow–Hassidim–Lloyd (HHL) algorithm with a classical computer, and designed a hybrid HHL algorithm to reduce experimental errors caused by decoherence and so on. However, the improvement is achieved only in the auxiliary quantum coding phase, and no quantum resource reduction is done on the quantum phase estimation and inverse quantum phase estimation stages. At the same time, the circuit improvement illustration on a \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$2\times 2$$\end{document}2×2 linear system just has the result and no specific process. In this paper, based on the idea of the hybrid HHL algorithm and a generic circuit of HHL algorithm, an improved circuit implementation of the HHL algorithm is proposed. The feasibility of the improved circuit implementation of the HHL algorithm is verified by IBM's qiskit. The improved circuit illustrations on a \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$4\times 4$$\end{document}4×4 linear system show that the improved circuit implementation of the HHL algorithm can effectively reduce quantum resources without losing the fidelity of the results. Thus the improved circuit implementation of the HHL algorithm can further avoid some result errors than the existing implementation methods.

algorithm with a classical computer, and designed a hybrid HHL algorithm to reduce experimental errors caused by decoherence and so on. However, the improvement is achieved only in the auxiliary quantum coding phase, and no quantum resource reduction is done on the quantum phase estimation and inverse quantum phase estimation stages. At the same time, the circuit improvement illustration on a 2 × 2 linear system just has the result and no specific process. In this paper, based on the idea of the hybrid HHL algorithm and a generic circuit of HHL algorithm, an improved circuit implementation of the HHL algorithm is proposed. The feasibility of the improved circuit implementation of the HHL algorithm is verified by IBM's qiskit. The improved circuit illustrations on a 4 × 4 linear system show that the improved circuit implementation of the HHL algorithm can effectively reduce quantum resources without losing the fidelity of the results. Thus the improved circuit implementation of the HHL algorithm can further avoid some result errors than the existing implementation methods.
Quantum computing is an operation method that follows the operating laws of quantum mechanics. Compared with traditional classical computing, quantum computing achieves an exponential speedup on some problems. For example, the Shor quantum algorithm 1 is famous for factoring large integers in polynomial time. Grover algorithm 2 achieves an exponential speedup in searching data. While the HHL algorithm 3 for solving linear systems of equations with exponential speedup over the best known classical algorithms.
However, during the operation of quantum circuits, due to the limitations of current technology, the errors of quantum gates, experimental errors and decoherence will introduce errors in the experimental process [4][5][6] . Therefore, it is necessary to reduce the number of gates and the overall running time of the algorithm. In view of this, the realization and improvement of quantum circuits has attracted the attention of various fields.
Among them, in terms of quantum circuit implementation and improvement of cryptographic algorithms, Langenberg et al. proposed the quantum circuit implementation and improvement of AES cryptographic algorithm 7 , and many scholars have made further improvements on it 8,9 . In circuit synthesis of quantum algorithms, Monz et al. implemented the use of Shor's algorithm to factorize numbers 15 10 using 7 qubits and 4 cache qubits with efficient control. Diao et al. proposed the quantum circuit composition of Grover algorithm 11 . Markus Grassl successfully used the Grover algorithm to realize the exhaustive key search for AES 12 . The quantum circuit implementation and improvement of the HHL algorithm also attracted a lot of attention. Among them, by introducing the variable time amplitude amplification algorithm into the HHL algorithm, Ambainis et al. reduced the number of repeated runs required to obtain the correct answer, thereby reducing the running time of the algorithm 13 , but did not give a specific quantum circuit implementation. Yudong Cao et al. proposed a generic circuit of HHL algorithm 14 . Yonghae Lee et al. gave a hybrid HHL quantum algorithm 6 by combining with classical computers, which effectively reduces the quantum gate resources used in the auxiliary qubit rotation part of the HHL algorithm. Compared with the generic circuit of HHL algorithm proposed by Cao et al., the hybrid HHL algorithm uses smaller quantum resources in the auxiliary qubit rotation stage. However, the improvement has only limited to the auxiliary quantum encoding stage, and no quantum resource can be reduced on the quantum phase estimation and inverse quantum phase estimation stages. Meanwhile, in the circuit improvement verification, there is only improvement result on a 2 × 2 linear system and no specific realization process is given.
In view of this, inspired by the idea of combining with classical computers and the generic circuit of HHL algorithm, we provide an improved circuit implementation of the HHL algorithm to further reducing the number of quantum gates, thus further avoiding some result errors caused by quantum gate errors. www.nature.com/scientificreports/ results show that our improved circuit implementation of the HHL algorithm effectively reduces the consumption of quantum resources without losing the fidelity of the results.

Basic definitions.
• k-fixed 6 : Suppose j , j = 1, . . . , l, is all non-zero eigenvalues of the Hermitian matrix A, b j k is the kth bit of the binary representation of the eigenvalues j , j = 1, …, l, defined m k , k ∈ N , as follows Hermitian matrix A is said to be k-fixed if it is m k fixed at 0 or 1.
• n-estimated 6 : Denoted as the eigenvalue of the matrix, if it can be represented by no more than n binary number, it is called n-estimated. • fidelity f 15  where A is m × n matrix: The above mentioned is a system of linear equations in the classical algorithm, which is expressed in quantum computation as follows: where |x � and |b � are quantum states.
The HHL algorithm is used to solve quantum equations. However, when using HHL algorithm to solve the problem, some specific conditions still need to be met. Matrix A must be an n × n square matrix, and A must be a Hermitian operator. If not, A needs to be transformed into a Hermitian matrix in some way. The core idea of HHL algorithm includes phase estimation, controlled rotation and inverse phase estimation.
The general steps of the HHL algorithm are: (1) First, initialize the quantum state |b �.
(2) The quantum phase estimation method is used to decompose |b � , into the superposition of the linear combination of eigenvectors of A. (3) After inverting the matrix A, we get the quantum state A −1 |x � = |b �. (4) Canceling the eigenvalues stored in the register by using the inverse quantum phase estimation method.
Quantum circuit implementation of HHL algorithm. The quantum circuit implementation of the HHL algorithm requires the use of three quantum registers, denoted as Ancilla , Reg.C and Reg.B , where • Ancilla is used to store auxiliary qubits; • Reg.C is used to store the binary representation of the eigenvalues of the coefficient matrix A; • Reg.B is used to store the vector solution of a system of linear equations when the measurement of the contents of the Ancilla quantum register is 1. • Initially, all three quantum registers are set to |0 � state.
As shown in Fig. 1, the quantum circuit implementation process of the HHL algorithm is mainly composed of three stages, namely: quantum phase estimation (Quantum Phase Estimation, QPE), auxiliary quantum encoding (Ancilla quantum encoding, AQE) and inverse quantum phase estimation (Inverse Quantum Phase Estimation, Inverse QPE) of which (a) QPE, that is, Quantum Phase Estimation. In the HHL algorithm, if the quantum register is measured after the QPE stage, then the binary representation of the eigenvalues of the linear equation system will be obtained, denoted as |θ 0 θ 1 ...θ n−1 �. Firstly, in the preparation phase, the first n qubits are initialized to 0, and the last m qubits are initialized to the quantum state |u � , so the initial state of the register is |0 � ⊗n |u � , then apply the H gate to n qubits. www.nature.com/scientificreports/ Secondly, using n controlled U gates, add the phase e 2πiθ to the probability amplitude.
Finally, the inverse QFT (quantum Fourier transform) is performed on the |ϕ 2 � quantum state. This step extracts the target value θ from the probability amplitude to the ground state. For qubits |x k �, k = 1, 2, . . . n.
To extract θ k into the quantum state, it is necessary to perform phase rotation on the qubit |x k � before applying the H gate to the qubit |x k � , and the phase selection angle is e Extract all |θ 0 θ 1 ...θ n−1 � into qubits to get the binary representation of the eigenvalues of the matrix Measure the result to get |θ 0 θ 1 ...θ n−1 � , which is the binary representation of the eigenvalues of the desired matrix.
In the entire HHL algorithm, the unitary operator U = U A = e iAt = N−1 j=0 e i j t u j � �u j , |b � = N−1 j=0 b j u j � , where t is a constant that can be set by itself. Therefore, in the entire HHL algorithm, the state of time, that is, the moment in Fig. 1

is (b):
Here j nl is the n-bit binary representation of " j ". (b) In the AQE stage, a controlled rotation operation on auxiliary qubits is performed as follows: Here c is a normalizing constant. After the AQE part, the state of the system at moment Fig. 1c is: www.nature.com/scientificreports/ (c) Inverse QPE stage is the inverse operation of the QPE stage. After the Inverse QPE stage, the j in the superposition state in the Reg.C register will become |0 � , at this time, the state of the entire quantum system is Fig. 1 (d): After the above three stages are completed, the status in the Reg.C register changes to the |0 � ⊗n state, and the auxiliary qubit is measured on the Z-axis. If the outcome is 1, the register is in the post-measurement state: which up to a normalisation factor corresponds to the solution. Hybrid HHL algorithm. Yonghae Lee et al. proposed a hybrid HHL algorithm in 2019 6 . The implementation of the algorithm is based on the following two characteristics given in the paper: For example, for a 2 × 2 matrix, at that time n=2 = 1, 2 or 3 , its eigenvalues are 2-estimated. Using 2-bit quantum registers, its fidelity can reach 1, but if its eigenvalues exceed 3 or are decimals, it is not perfectly 2-estimated, so the fidelity cannot reach 1 when using 2-bit quantum registers.

Characteristic 2.
If the eigenvalues of the matrix are not perfectly n-estimated, then the fidelity of the algorithm's results will be positively related to the number of extra quantum registers used. This is because for matrices with non-perfect n-estimated eigenvalues, additional qubit registers are needed to represent the eigenvalues of the matrix and to control the phase rotation. In other words, additional quantum are required to improve the fidelity of the results. For example, the eigenvalues of a 2 × 2 matrix n=2 = 4 are not perfectly represented by 2-estimated. In this case, in order to make the fidelity close to 1, it is necessary to use 3 qubits to store the eigenvalue information of the matrix.
From Characteristic 1 and Characteristic 2 we note that once the eigenvalues of the matrix are not perfectly n-estimated, the number of qubits in quantum registers that need to be used to achieve high fidelity increases dramatically.When all eigenvalues can be perfectly n-estimated, n-scale quantum registers are also required to ensure high fidelity. Therefore, in the implementation process of the HHL algorithm, in order to reduce the circuit complexity, it is necessary to choose a matrix with perfect n-estimated as much as possible. At the same time, the literature 6 pointed out the following conclusion.

Conclusion.
In the execution of the HHL algorithm, for a perfect n-estimated matrix, if it has k-fixed eigenvalues, it can be implemented with a smaller depth quantum circuit, and compared with the original HHL algorithm, the circuit complexity is reduced, and the smaller depth quantum circuit of HHL algorithm has higher fidelity.
Reference 6 uses the above conclusion to improve the quantum circuit of the AQE stage of the HHL algorithm, and gives the improved quantum circuit of the 2 × 2 matrix as shown in Fig. 3. In the hybrid HHL algorithm, the feedforward combined with the information obtained by classical calculation after quantum phase estimation effectively reduces the number of quantum gates of the original HHL algorithm.
However, the implementation details of the improvement of the AQE stage of the HHL algorithm are not described in this algorithm, and the quantum circuits in the QPE and inverse QPE stages are not improved. This paper will take the generic circuit of HHL algorithm proposed by Yudong Cao et al. as the framework, and use the design idea of the hybrid HHL algorithm proposed by Yonghae Lee et al. to improve the implementation circuit of the HHL algorithm as a whole.

Result Preparation before implementation. QPE is repeatedly performed to obtain the information of the eigen-
values: in the quantum phase estimation part, the binary representation of the eigenvalues has been stored in Reg.B each qubit of the quantum register after the phase estimation. If the Reg.B quantum register is measured on the Z-axis, then the Reg.B quantum register can be collapsed to an eigenvalue of the matrix, and the measurement process can be repeated 6 .
Secondly, the prior information is obtained by combining probability statistics with classical computers: whether the information of the matrix eigenvalues obtained in the QPE stage is statistically observed to be k-fixed. If the k-fixed characteristic is not observed in the statistical eigenvalue information, more qubit registers need to be used to store the matrix eigenvalues in order to discover the k-fixed characteristic.
Simplification of quantum circuits. In this subsection, according to the k-fixed property of the matrix, we give a specific implementation method to reduce the number of quantum gates required to realize the HHL quantum circuit, and compare it with the generic circuit of HHL algorithm.
(1) Quantum phase estimation stage When the eigenvalue of the matrix, that is, is k-fixed, the kth binary value m k of all eigenvalues of the matrix is either 0 or 1. According to our definition of m k , the state of Eq. (13) is the same as the state of Eq. (3) at this time. Secondly, when using n − 1 controlled gate to add phase e 2πiθ to the probability amplitude, it does not have to act on the kth qubit.
The state of Eq. (14) is the same as the state of Eq. (4) at this time. Finally, |ϕ 2 � quantum state performs inverse quantum Fourier transform, which extracts the target value θ from the probability amplitude into the ground state.
For qubits |x k �, k = 1, 2, ...n . We already know that the matrix is k-fixed and m k = 0 , so there is no need to adjust |x k � perform phase rotation and directly measure |x k � , we can get |0 �.
The state of Eq. (15) is also the same as the state of Eq. (7) at this time. When the matrix is k-fixed and m k = 0 . According to the definition of m k , the QPE part of the improved circuit implementation of the HHL algorithm is the same as the QPE part of the original HHL algorithm. For m k = 1 , it is similar to the process of m k = 0 , except that in the first stage of QPE, the initialization state of the kth qubit is changed to |1 �.
For example, when the eigenvalue of a perfect 4-estimated matrix is j 2-fixed, one can get According to the above formula, the comparison diagram shown in Fig. 4 can be obtained. Thus, if the eigenvalues of a perfect n-estimated matrix satisfy k-fixed. For a qubit representing k in quantum register Reg.C , the applied quantum gate can be roughly reduced by a factor of n−1 n . (2) Auxiliary quantum encoding phase After phase estimation, quantum register Reg.C stores a series of binary superposition states of eigenvalues. The control rotation part is to control the auxiliary qubit according to the superposition state in the quantum register at this time, as shown in Fig. 5 below. Figure 5a is the quantum circuit under the generic circuit of HHL algorithm. Figure 5b is 2-fixed and m 1 = 0 improved quantum circuit implementation. Figure 5c is the improved quantum circuit implementation of 2-fixed and m 1 = 1.
After the AQE section, we can get the system status as follows: (3) Inverse quantum phase estimation (13)   www.nature.com/scientificreports/ After the AQE part, the inverse quantum phase estimation and quantum phase estimation have the same simplified circuit implementation, and will not be repeated here. The applied quantum gates can likewise be roughly reduced to the original n−1 n .
Quantum circuit implementation example. IBM Q provides a Qiskit library based on the Python programming environment that can be used for remote access or emulation with classics. In this section, the Qiskit library is used to simulate   www.nature.com/scientificreports/ circuit implementation of the HHL algorithm. To obtain the result we run the simulation 1024 times. Here the eigenvalues of matrix A can be accurately stored using four qubits. The quantum circuit is shown in Fig. 6 below. It can be seen from Fig. 6 that the improved circuit implementation of the HHL algorithm uses less quantum gates than the generic circuit of HHL algorithm. On the other hand, the quantum resources and fidelity used to obtain the simulation solution using the generic circuit of HHL algorithm and the simulation solution obtained through the improved circuit implementation of the HHL algorithm are compared as shown in Table 1 below.
Experimental results show that when using improved circuit to solve linear equations, the fidelity of the experimental solution is higher than that of the original HHL general purpose quantum circuit, and it uses less quantum resources.

Discussion
In general, for linear systems, if the prior condition is satisfied, that is, the eigenvalues of the matrix of the linear equation system have k-fixed characteristics, the quantum resources consumption can be reduced without reducing the fidelity of the experimental results. For an n-dimensional linear equation system, in the case of satisfying k-fixed, the quantum gate applied to the qubit representing k in the quantum register Reg.C can be roughly reduced to the original n−1 n . If we observe more k-fixed information in the first step, and record the number of k-fixed as k num , 0 < k num <= n , the applied quantum gates can be roughly reduced to the original n−k num n , and the reduced circuit depth of the quantum circuit is about 2n.
a Generic circuit of HHL algorithm b Improved circuit implementation of the HHL algorithm

Methods
Improved circuit implementation of the HHL algorithm is the same as the generic circuit of HHL algorithm in the initialization part, the difference is that the improved circuit implementation of the HHL algorithm uses the generic circuit of HHL algorithm to achieve the QPE part, and further uses the measured Reg.B quantum register. The information of some matrix eigenvalues is used to assist the construction of the following circuit.

Data availability
The datasets used and/or analysed during the current study available from the corresponding author on reasonable request.